﻿using System;
using System.Collections.Generic;
using System.Text;
using ZhCun.Framework.DataAccess.DAL;
using ZhCun.Framework.Common.Models.TableModel;
using ZhCun.Framework.Common.Models;
using System.Data;
using System.Data.Common;
using ZhCun.Framework.Common;
using ZhCun.Framework.Common.Models.ProcedureModel;
using ZhCun.Framework.DataAccess.DataDelegate;
using ZhCun.Framework.Common.Models.WhereHelperModel;

namespace ZhCun.Framework.DataAccess.BLL
{
    /// <summary>
    /// 这个BLLBase封装了WhereHelper
    /// </summary>
    public abstract class BLLBase
    {
        public BLLBase()
        { }
        /// <summary>
        /// dal 需要一个所对应表的DAL对象
        /// </summary>
        public BLLBase(DalBase dal)
        {
            this._DAL = dal;
        }
        /// <summary>
        /// 指定数据库类型,不再需要dal对象了
        /// </summary>
        /// <param name="dbType"></param>
        /// <param name="connStr"></param>
        public BLLBase(DatabaseTypeEnum dbType, string connStr)
        {
            this._DAL = DALFactory.CreateDal(dbType, connStr);
        }

        protected DalBase _DAL;

        #region 规则事件们

        public event BLLModelHandle InsertBeforeHandle;
        //public event BLLModelHandle InsertAfterHandle;

        #endregion

        #region 获得where查询数据参数的方法

        /// <summary>
        /// 根据参数实体,获得where条件及参数值对象(主方法)
        /// </summary>
        protected void GetSqlWhereParam(ref StringBuilder sqlWhere, ref Dictionary<string, object> paramDict, WhereHelper wh)
        {
            if (sqlWhere == null) sqlWhere = new StringBuilder();
            if (paramDict == null) paramDict = new Dictionary<string, object>();
            if (wh == null) return;
            sqlWhere.Append(wh);
            foreach (var item in wh.ParamDict.Keys)
            {
                paramDict.Add(item, wh.ParamDict[item]);
            }
        }

        #endregion

        #region 获取数据库时间

        /// <summary>
        /// 供子类使用的过的当前时间,这个方法通常是返回服务器的时间方法
        /// </summary>
        /// <returns></returns>
        public DateTime GetCurrentTime()
        {
            return _DAL.GetNowTime();
        }

        #endregion

        #region 创建WhereHelper对象

        /// <summary>
        /// 创建CreateWhereHelper
        /// </summary>
        public WhereHelper CreateWhereHelper()
        {
            return _DAL.CreateWhereHelper();
        }

        #endregion

        #region 事务的操作

        public void TransStart()
        {
            _DAL.TransStart();
        }
        public void TransCommit()
        {
            _DAL.TransCommit();
        }
        public void TransRollback()
        {
            _DAL.TransRollback();
        }

        #endregion

        #region 增加,删除,修改

        public virtual int ExecuteBySQL(string sqlText, Dictionary<string, object> dbParams)
        {
            return _DAL.ExecuteBySQL(sqlText, dbParams);
        }

        public virtual int Add<TModel>(params TModel[] models) where TModel : ModelBase, new()
        {
            if (InsertBeforeHandle != null)
            {
                InsertBeforeHandle(models);
            }
            int r = _DAL.Add(models);

            return r;
        }

        #region 更新的几个重载

        /// <summary>
        /// 更新的第一层方法
        /// </summary>
        /// <param name="tranId">事务id</param>
        /// <param name="fields">要更新的字段</param>
        /// <param name="sqlWhere">sqlWhere条件</param>
        /// <param name="dbParams">参数</param>
        /// <param name="models">要更新的实体</param>
        /// <returns>返回所影响行数</returns>
        public virtual int Update<TModel>(string[] fields, string sqlWhere, Dictionary<string, object> dbParams, params TModel[] models) where TModel : ModelBase, new()
        {
            return _DAL.Update(fields, sqlWhere, dbParams, models);
        }

        public int Update<TModel>(string[] fields, params TModel[] models) where TModel : ModelBase, new()
        {
            return Update(fields, string.Empty, null, models);
        }

        public int Update<TModel>(string[] fields, WhereHelper dbWhereModel, params TModel[] models) where TModel : ModelBase, new()
        {
            StringBuilder sqlWhere = null;
            Dictionary<string, object> dbParams = null;
            GetSqlWhereParam(ref sqlWhere, ref dbParams, dbWhereModel);
            return Update<TModel>(fields, sqlWhere.ToString(), dbParams, models);
        }

        #endregion

        public virtual int Delete<TModel>(params object[] pkValues) where TModel : ModelBase, new()
        {
            return _DAL.Delete<TModel>(pkValues);
        }

        #endregion

        #region 判断字段值是否存在

        public bool IsExist<TModel>(string fieldName, object fieldValue, WhereHelper dbWhere) where TModel : ModelBase, new()
        {
            StringBuilder sqlWhere = null;
            Dictionary<string, object> dbParam = null;
            GetSqlWhereParam(ref sqlWhere, ref dbParam, dbWhere);
            return _DAL.IsExist<TModel>(null, fieldName, fieldValue, sqlWhere.ToString(), dbParam);
        }

        public bool IsExist<TModel>(string fieldName, object fieldValue) where TModel : ModelBase, new()
        {
            return IsExist<TModel>(fieldName, fieldValue, null);
        }

        public bool IsExist<TModel>(WhereHelper wh) where TModel : ModelBase, new()
        {
            return IsExist<TModel>(null, null, wh);
        }

        #endregion

        #region 获得首行首列的方法

        /// <summary>
        /// 根据主键获得一个指定字段的值
        /// </summary>
        public virtual object GetObjectByField<TModel>(string fieldName, object pkValue) where TModel : ModelBase, new()
        {
            return _DAL.GetObjectByField<TModel>(fieldName, pkValue);
        }

        #endregion

        #region 根据主键获得一个实体

        public virtual TModel GetModel<TModel>(object id) where TModel : ModelBase, new()
        {
            return _DAL.GetModel<TModel>(id);
        }
        #endregion

        #region 根据条件获得一个实体
        public virtual TModel GetModel<TModel>(WhereHelper whereModel) where TModel : ModelBase, new()
        {
            StringBuilder sqlWhere = null;
            Dictionary<string, object> dbParams = null;
            GetSqlWhereParam(ref sqlWhere, ref dbParams, whereModel);
            return _DAL.GetModel<TModel>(sqlWhere.ToString(), dbParams);
        } 
        #endregion

        #region 获得记录数

        public virtual int GetRecordCount<TModel>(string sqlWhere, Dictionary<string, object> dbParams) where TModel : ModelBase, new()
        {
            return _DAL.GetRecordCount<TModel>(sqlWhere, dbParams);
        }
        public int GetRecordCount<TModel>() where TModel : ModelBase, new()
        {
            return GetRecordCount<TModel>(string.Empty, null);
        }
        public int GetRecordCount<TModel>(WhereHelper whereModel) where TModel : ModelBase, new()
        {
            StringBuilder sqlWhere = null;
            Dictionary<string, object> dbParams = null;
            GetSqlWhereParam(ref sqlWhere, ref dbParams, whereModel);
            return GetRecordCount<TModel>(sqlWhere.ToString(), dbParams);
        }

        #endregion

        #region 设置搜索的注册搜索规则

        //注册搜索参数
        void RegisterSearchParam<TModel>(string sqlWhere, Dictionary<string, object> dbParams) where TModel : ModelBase, new()
        {
            //if (SearchBefore != null)
            //{
            //    Type type;
            //    DbWhereModel[] searchWhereParam;
            //    SearchBefore(out type, out searchWhereParam);
            //    if (typeof(TModel) == type)
            //    {
            //        if (searchWhereParam != null && searchWhereParam.Length > 0)
            //        {
            //            StringBuilder sbWhere = null;
            //            if (sqlWhere != null && sqlWhere.Trim().Length > 1)
            //            {
            //                sbWhere = new StringBuilder(sqlWhere);
            //            }
            //            GetSqlWhereParam(ref sbWhere, ref dbParams, searchWhereParam);
            //            sqlWhere = sbWhere.ToString();
            //        }
            //    }
            //    //    DbWhereModel[] searchWhereParam;
            //    //    SearchBefore(out searchWhereParam);
            //    //    if (searchWhereParam != null && searchWhereParam.Length > 0)
            //    //    {
            //    //        StringBuilder sbWhere = null;
            //    //        if (sqlWhere != null && sqlWhere.Trim().Length > 1)
            //    //        {
            //    //            sbWhere = new StringBuilder(sqlWhere);
            //    //        }
            //    //        GetSqlWhereParam(ref sbWhere, ref dbParams, searchWhereParam);
            //    //        sqlWhere = sbWhere.ToString();
            //    //    }
            //}
        }

        #endregion

        #region 获得DataTable

        public virtual DataTable GetDataTableBySQL(string sql, Dictionary<string, object> dbParams)
        {
            return _DAL.GetDataTableBySQL(sql, dbParams);
        }

        #region 条件查询的几个重载(不分页)

        /// <summary>
        /// 主方法,其他重载都由此方法而生
        /// </summary>
        /// <param name="sqlWhere">SQL where条件</param>
        /// <param name="dbParams">sql文本中所对应的参数</param>
        /// <param name="orderFields">排序字段</param>
        /// <param name="isDesc">是否降序,false:为升序</param>
        /// <param name="selectFields">要显示的字段,select 查询</param>
        /// <returns>返回DataTable</returns>
        public virtual DataTable GetDataTable<TModel>(string sqlWhere, Dictionary<string, object> dbParams, string[] orderFields, bool isDesc, params string[] selectFields) where TModel : ModelBase, new()
        {
            RegisterSearchParam<TModel>(sqlWhere, dbParams);
            return _DAL.GetDataTable<TModel>(sqlWhere, dbParams, orderFields, isDesc, selectFields);
        }

        public virtual DataTable GetDataTable<TModel>(string[] selectFields, string[] orderFields, bool isDesc, WhereHelper dbWhereModel) where TModel : ModelBase, new()
        {

            StringBuilder sqlWhere = null;
            Dictionary<string, object> dbParam = null;
            GetSqlWhereParam(ref sqlWhere, ref dbParam, dbWhereModel);
            return GetDataTable<TModel>(sqlWhere.ToString(), dbParam, orderFields, isDesc, selectFields);
        }

        public virtual DataTable GetDataTable<TModel>(string[] selectFields, WhereHelper dbWhereModel) where TModel : ModelBase, new()
        {
            StringBuilder sqlWhere = null;
            Dictionary<string, object> dbParam = null;
            GetSqlWhereParam(ref sqlWhere, ref dbParam, dbWhereModel);
            return GetDataTable<TModel>(sqlWhere.ToString(), dbParam, null, false, selectFields);
        }

        public virtual DataTable GetDataTable<TModel>(WhereHelper dbWhereModel) where TModel : ModelBase, new()
        {
            return GetDataTable<TModel>(null, null, false, dbWhereModel);
        }
        public virtual DataTable GetDataTable<TModel>() where TModel : ModelBase, new()
        {
            return GetDataTable<TModel>(null, null, null, false, null);
        }

        #endregion

        #region 获得分页的几个重载

        public virtual DataTable GetDataTableByPage<TModel>(int onePageCount, int pageNo, bool isDesc, string[] orderField, out int pageCount, out int recordCount, string sqlWhere, Dictionary<string, object> dbParams, params string[] selectFields) where TModel : ModelBase, new()
        {
            RegisterSearchParam<TModel>(sqlWhere, dbParams);
            return _DAL.GetDataTableByPage<TModel>(onePageCount, pageNo, isDesc, orderField, out pageCount, out recordCount, sqlWhere, dbParams, selectFields);
        }


        public virtual DataTable GetDataTableByPage<TModel>(string[] selectFields, DbPagingModel padingModel, WhereHelper dbWhereModel) where TModel : ModelBase, new()
        {
            int pageCount, recordCount;
            StringBuilder sqlWhere = null;
            Dictionary<string, object> dbParam = null;
            GetSqlWhereParam(ref sqlWhere, ref dbParam, dbWhereModel);
            DataTable dt = GetDataTableByPage<TModel>(
                padingModel.OnePageCount,
                padingModel.PageNo,
                padingModel.IsDesc,
                padingModel.OrderField,
                out pageCount,
                out recordCount,
                sqlWhere.ToString(), dbParam, selectFields);
            padingModel.PageCount = pageCount;
            padingModel.RecordCount = recordCount;
            return dt;
        }
        public virtual DataTable GetDataTableByPage<TModel>(string[] selectFields, DbPagingModel padingModel) where TModel : ModelBase, new()
        {
            int pageCount, recordCount;
            DataTable dt = GetDataTableByPage<TModel>(
                padingModel.OnePageCount,
                padingModel.PageNo,
                padingModel.IsDesc,
                padingModel.OrderField,
                out pageCount,
                out recordCount,
                null, null, selectFields);
            padingModel.PageCount = pageCount;
            padingModel.RecordCount = recordCount;
            return dt;
        }

        public virtual DataTable GetDataTableByPage<TModel>(DbPagingModel padingModel, WhereHelper dbWhereModel) where TModel : ModelBase, new()
        {
            return GetDataTableByPage<TModel>(null, padingModel, dbWhereModel);
        }
        public virtual DataTable GetDataTableByPage<TModel>(DbPagingModel padingModel) where TModel : ModelBase, new()
        {
            int pageCount, recordCount;
            DataTable dt = GetDataTableByPage<TModel>(
                padingModel.OnePageCount,
                padingModel.PageNo,
                padingModel.IsDesc,
                padingModel.OrderField,
                out pageCount,
                out recordCount,
                null, null, null);
            padingModel.PageCount = pageCount;
            padingModel.RecordCount = recordCount;
            return dt;
        }

        #endregion

        #region 获得前几行的几个重载方法

        public virtual DataTable GetDataTableTopCount<TModel>(int topCount, string sqlWhere, Dictionary<string, object> dbParams, string[] orderFields, bool isDesc, params string[] selectFields) where TModel : ModelBase, new()
        {
            RegisterSearchParam<TModel>(sqlWhere, dbParams);
            return _DAL.GetDataTableTopCount<TModel>(topCount, sqlWhere, dbParams, orderFields, false, selectFields);
        }

        public virtual DataTable GetDataTableTopCount<TModel>(string[] selectFields, int topCount, string[] orderFields, bool isDesc, WhereHelper dbWhereModel) where TModel : ModelBase, new()
        {
            StringBuilder sqlWhere = null;
            Dictionary<string, object> dbParam = null;
            GetSqlWhereParam(ref sqlWhere, ref dbParam, dbWhereModel);
            return GetDataTableTopCount<TModel>(topCount, sqlWhere.ToString(), dbParam, orderFields, isDesc, selectFields);
        }
        public virtual DataTable GetDataTableTopCount<TModel>(string[] selectFields, int topCount, string[] orderFields, bool isDesc) where TModel : ModelBase, new()
        {
            //StringBuilder sqlWhere = null;
            //Dictionary<string, object> dbParam = null;
            //GetSqlWhereParam(ref sqlWhere, ref dbParam, dbWhereModel);
            return GetDataTableTopCount<TModel>(topCount, null, null, orderFields, isDesc, selectFields);
        }


        public virtual DataTable GetDataTableTopCount<TModel>(string[] selectFields, int topCount, WhereHelper dbWhereModel) where TModel : ModelBase, new()
        {
            return GetDataTableTopCount<TModel>(selectFields, topCount, null, false, dbWhereModel);
        }
        public virtual DataTable GetDataTableTopCount<TModel>(string[] selectFields, int topCount) where TModel : ModelBase, new()
        {
            return GetDataTableTopCount<TModel>(selectFields, topCount, null, false);
        }

        public virtual DataTable GetDataTableTopCount<TModel>(int topCount, WhereHelper dbWhereModel) where TModel : ModelBase, new()
        {
            return GetDataTableTopCount<TModel>(null, topCount, null, false, dbWhereModel);
        }
        public virtual DataTable GetDataTableTopCount<TModel>(int topCount) where TModel : ModelBase, new()
        {
            return GetDataTableTopCount<TModel>(null, topCount, null, false);
        }


        #endregion

        #endregion

        #region 获取DataReader

        #region 条件查询的几个重载

        public virtual DbDataReader GetDataReaderBySQL(string sql, Dictionary<string, object> dbParams)
        {
            return _DAL.GetDataReaderBySQL(sql, dbParams);
        }
        //其它重载方法由此方法衍生
        public DbDataReader GetDataReader<TModel>(string sqlWhere, Dictionary<string, object> dbParams, string[] orderFields, bool isDesc, params string[] selectFields) where TModel : ModelBase, new()
        {
            RegisterSearchParam<TModel>(sqlWhere, dbParams);
            return _DAL.GetDataReader<TModel>(sqlWhere, dbParams, orderFields, isDesc);
        }

        #region 执行DataReader的逐行读取操作,交由一个委托来处理每行数据

        //执行DataReader交个一个委托来处理
        protected virtual void ExecDataReader(DbDataReader dbReader, DbDataReaderHandle ReaderHandle)
        {
            using (dbReader)
            {
                if (dbReader.HasRows)
                {
                    bool isNewRow = false;
                    while (dbReader.Read())
                    {
                        for (int i = 0; i < dbReader.FieldCount; i++)
                        {
                            string cName = dbReader.GetName(i);
                            //dbDetail.Add(cName, dbReader[i]);
                            isNewRow = i == dbReader.FieldCount - 1 ? true : false;
                            ReaderHandle(cName, dbReader[i], isNewRow);
                        }
                    }
                }
            }
        }

        public void ExecDataReader<TModel>(DbDataReaderHandle ReaderHandle, string[] selectFields, string[] orderFields, bool isDesc, WhereHelper dbWhereModel) where TModel : ModelBase, new()
        {
            StringBuilder sqlWhere = null;
            Dictionary<string, object> dbParam = null;
            GetSqlWhereParam(ref sqlWhere, ref dbParam, dbWhereModel);
            DbDataReader reader = GetDataReader<TModel>(sqlWhere.ToString(), dbParam, orderFields, isDesc, selectFields);
            ExecDataReader(reader, ReaderHandle);
        }

        public void ExecDataReader<TModel>(DbDataReaderHandle ReaderHandle, string[] selectFields, string[] orderFields, bool isDesc) where TModel : ModelBase, new()
        {
            DbDataReader reader = GetDataReader<TModel>(null, null, orderFields, isDesc, selectFields);
            ExecDataReader(reader, ReaderHandle);
        }

        public void ExecDataReader<TModel>(DbDataReaderHandle ReaderHandle, string[] selectFields, WhereHelper dbWhereModel) where TModel : ModelBase, new()
        {
            ExecDataReader<TModel>(ReaderHandle, selectFields, null, false, dbWhereModel);
        }

        public void ExecDataReader<TModel>(DbDataReaderHandle ReaderHandle, string[] selectFields) where TModel : ModelBase, new()
        {
            ExecDataReader<TModel>(ReaderHandle, selectFields, null, false);
        }

        public void ExecDataReader<TModel>(DbDataReaderHandle ReaderHandle, WhereHelper dbWhereModel) where TModel : ModelBase, new()
        {
            ExecDataReader<TModel>(ReaderHandle, null, null, false, dbWhereModel);
        }

        public void ExecDataReader<TModel>(DbDataReaderHandle ReaderHandle) where TModel : ModelBase, new()
        {
            ExecDataReader<TModel>(ReaderHandle, null, null, false);
        }

        #endregion

        #endregion

        #region 获得分页数据的几个重载

        public virtual DbDataReader GetDataReaderByPage<TModel>(int onePageCount, int pageNo, bool isDesc, string[] orderField, out int pageCount, out int recordCount, string sqlWhere, Dictionary<string, object> dbParams, params string[] selectFields) where TModel : ModelBase, new()
        {
            RegisterSearchParam<TModel>(sqlWhere, dbParams);
            return _DAL.GetDataReaderByPage<TModel>(onePageCount, pageNo, isDesc, orderField, out pageCount, out recordCount, sqlWhere, dbParams, selectFields);
        }

        public void GetDataReaderByPage<TModel>(DbDataReaderHandle ReaderHandle, string[] selectFields, DbPagingModel padingModel, WhereHelper dbWhereModel) where TModel : ModelBase, new()
        {
            int pageCount, recordCount;
            StringBuilder sqlWhere = null;
            Dictionary<string, object> dbParam = null;
            GetSqlWhereParam(ref sqlWhere, ref dbParam, dbWhereModel);
            DbDataReader reader = GetDataReaderByPage<TModel>(
                padingModel.OnePageCount,
                padingModel.PageNo,
                padingModel.IsDesc,
                padingModel.OrderField,
                out pageCount,
                out recordCount,
                sqlWhere.ToString(), dbParam, selectFields);
            padingModel.PageCount = pageCount;
            padingModel.RecordCount = recordCount;
            ExecDataReader(reader, ReaderHandle);
        }
        public void GetDataReaderByPage<TModel>(DbDataReaderHandle ReaderHandle, string[] selectFields, DbPagingModel padingModel) where TModel : ModelBase, new()
        {
            int pageCount, recordCount;
            DbDataReader reader = GetDataReaderByPage<TModel>(
                padingModel.OnePageCount,
                padingModel.PageNo,
                padingModel.IsDesc,
                padingModel.OrderField,
                out pageCount,
                out recordCount,
                null, null, selectFields);
            padingModel.PageCount = pageCount;
            padingModel.RecordCount = recordCount;
            ExecDataReader(reader, ReaderHandle);
        }


        public void GetDataReaderByPage<TModel>(DbDataReaderHandle ReaderHandle, DbPagingModel padingModel, WhereHelper dbWhereModel) where TModel : ModelBase, new()
        {
            GetDataReaderByPage<TModel>(ReaderHandle, null, padingModel, dbWhereModel);
        }
        public void GetDataReaderByPage<TModel>(DbDataReaderHandle ReaderHandle, DbPagingModel padingModel) where TModel : ModelBase, new()
        {
            GetDataReaderByPage<TModel>(ReaderHandle, null, padingModel);
        }

        #endregion

        #region 获得前几行的几个重载

        public virtual DbDataReader GetDataReaderTopCount<TModel>(int topCount, string sqlWhere, Dictionary<string, object> dbParams, string[] orderFields, bool isDesc, params string[] selectFields) where TModel : ModelBase, new()
        {
            RegisterSearchParam<TModel>(sqlWhere, dbParams);
            return _DAL.GetDataReaderTopCount<TModel>(topCount, sqlWhere, dbParams, orderFields, isDesc, selectFields);
        }

        public void GetDataReaderTopCount<TModel>(DbDataReaderHandle ReaderHandle, string[] selectFields, int topCount, string[] orderFields, bool isDesc, WhereHelper dbWhereModel) where TModel : ModelBase, new()
        {
            StringBuilder sqlWhere = null;
            Dictionary<string, object> dbParam = null;
            GetSqlWhereParam(ref sqlWhere, ref dbParam, dbWhereModel);
            DbDataReader reader = GetDataReaderTopCount<TModel>(topCount, sqlWhere.ToString(), dbParam, orderFields, isDesc, selectFields);
            ExecDataReader(reader, ReaderHandle);
        }
        public void GetDataReaderTopCount<TModel>(DbDataReaderHandle ReaderHandle, string[] selectFields, int topCount, string[] orderFields, bool isDesc) where TModel : ModelBase, new()
        {
            DbDataReader reader = GetDataReaderTopCount<TModel>(topCount, null, null, orderFields, isDesc, selectFields);
            ExecDataReader(reader, ReaderHandle);
        }

        public void GetDataReaderTopCount<TModel>(DbDataReaderHandle ReaderHandle, string[] selectFields, int topCount, WhereHelper dbWhereModel) where TModel : ModelBase, new()
        {
            GetDataReaderTopCount<TModel>(ReaderHandle, selectFields, topCount, null, false, dbWhereModel);
        }
        public void GetDataReaderTopCount<TModel>(DbDataReaderHandle ReaderHandle, string[] selectFields, int topCount) where TModel : ModelBase, new()
        {
            GetDataReaderTopCount<TModel>(ReaderHandle, selectFields, topCount, null, false);
        }

        public void GetDataReaderTopCount<TModel>(DbDataReaderHandle ReaderHandle, int topCount, WhereHelper dbWhereModel) where TModel : ModelBase, new()
        {
            GetDataReaderTopCount<TModel>(ReaderHandle, null, topCount, dbWhereModel);
        }
        public void GetDataReaderTopCount<TModel>(DbDataReaderHandle ReaderHandle, int topCount) where TModel : ModelBase, new()
        {
            GetDataReaderTopCount<TModel>(ReaderHandle, null, topCount);
        }

        #endregion

        #endregion

        #region 获得List集合的几个重载

        #region 根据where条件数据的几个重载

        //主方法,其他重载都由此派生
        public virtual List<TModel> GetList<TModel>(string sqlWhere, Dictionary<string, object> dbParams, string[] orderFields, bool isDesc, params string[] selectFields) where TModel : ModelBase, new()
        {
            RegisterSearchParam<TModel>(sqlWhere, dbParams);
            return _DAL.GetList<TModel>(sqlWhere, dbParams, orderFields, isDesc, selectFields);
        }

        public virtual List<TModel> GetList<TModel>(string[] selectFields, string[] orderFields, bool isDesc, WhereHelper dbWhereModel) where TModel : ModelBase, new()
        {
            StringBuilder sqlWhere = null;
            Dictionary<string, object> dbParam = null;
            GetSqlWhereParam(ref sqlWhere, ref dbParam, dbWhereModel);
            return GetList<TModel>(sqlWhere.ToString(), dbParam, orderFields, isDesc, selectFields);
        }
        public virtual List<TModel> GetList<TModel>(string[] selectFields, string[] orderFields, bool isDesc) where TModel : ModelBase, new()
        {
            return GetList<TModel>(null, null, orderFields, isDesc, selectFields);
        }

        public virtual List<TModel> GetList<TModel>(string[] selectFields, WhereHelper dbWhereModel) where TModel : ModelBase, new()
        {
            return GetList<TModel>(selectFields, null, false, dbWhereModel);
        }
        public virtual List<TModel> GetList<TModel>(string[] selectFields) where TModel : ModelBase, new()
        {
            return GetList<TModel>(selectFields, null, false);
        }

        public virtual List<TModel> GetList<TModel>(WhereHelper dbWhereModel) where TModel : ModelBase, new()
        {
            return GetList<TModel>(null, null, false, dbWhereModel);
        }
        public virtual List<TModel> GetList<TModel>() where TModel : ModelBase, new()
        {
            return GetList<TModel>(null, null, false);
        }

        #endregion

        #region 获得分页数据的几个重载
        //主方法
        public List<TModel> GetListByPage<TModel>(int onePageCount, int pageNo, bool isDesc, string[] orderField, out int pageCount, out int recordCount, string sqlWhere, Dictionary<string, object> dbParams, params string[] selectFields) where TModel : ModelBase, new()
        {
            RegisterSearchParam<TModel>(sqlWhere, dbParams);
            return _DAL.GetListByPage<TModel>(onePageCount, pageNo, isDesc, orderField, out pageCount, out recordCount, sqlWhere, dbParams, selectFields);
        }


        public List<TModel> GetListByPage<TModel>(string[] selectFields, DbPagingModel padingModel, WhereHelper dbWhereModel) where TModel : ModelBase, new()
        {
            int pageCount, recordCount;
            StringBuilder sqlWhere = null;
            Dictionary<string, object> dbParam = null;
            GetSqlWhereParam(ref sqlWhere, ref dbParam, dbWhereModel);
            List<TModel> list = GetListByPage<TModel>(
                padingModel.OnePageCount,
                padingModel.PageNo,
                padingModel.IsDesc,
                padingModel.OrderField,
                out pageCount,
                out recordCount,
                sqlWhere.ToString(), dbParam, selectFields);
            padingModel.PageCount = pageCount;
            padingModel.RecordCount = recordCount;
            return list;
        }
        public List<TModel> GetListByPage<TModel>(string[] selectFields, DbPagingModel padingModel) where TModel : ModelBase, new()
        {
            int pageCount, recordCount;
            List<TModel> list = GetListByPage<TModel>(
                padingModel.OnePageCount,
                padingModel.PageNo,
                padingModel.IsDesc,
                padingModel.OrderField,
                out pageCount,
                out recordCount,
                null, null, selectFields);
            padingModel.PageCount = pageCount;
            padingModel.RecordCount = recordCount;
            return list;
        }
        public List<TModel> GetListByPage<TModel>(DbPagingModel padingModel, WhereHelper dbWhereModel) where TModel : ModelBase, new()
        {
            return GetListByPage<TModel>(null, padingModel, dbWhereModel);
        }
        public List<TModel> GetListByPage<TModel>(DbPagingModel padingModel) where TModel : ModelBase, new()
        {
            return GetListByPage<TModel>(null, padingModel);
        }

        #endregion

        #region 获得前几行List的几个重载

        //主方法
        public virtual List<TModel> GetListTopCount<TModel>(int topCount, string sqlWhere, Dictionary<string, object> dbParams, string[] orderFields, bool isDesc, params string[] selectFields) where TModel : ModelBase, new()
        {
            RegisterSearchParam<TModel>(sqlWhere, dbParams);
            return _DAL.GetListTopCount<TModel>(topCount, sqlWhere, dbParams, orderFields, isDesc, selectFields);
        }

        public virtual List<TModel> GetListTopCount<TModel>(string[] selectFields, int topCount, string[] orderFields, bool isDesc, WhereHelper dbWhereModel) where TModel : ModelBase, new()
        {
            StringBuilder sqlWhere = null;
            Dictionary<string, object> dbParam = null;
            GetSqlWhereParam(ref sqlWhere, ref dbParam, dbWhereModel);
            return GetListTopCount<TModel>(topCount, sqlWhere.ToString(), dbParam, orderFields, isDesc, selectFields);
        }
        public virtual List<TModel> GetListTopCount<TModel>(string[] selectFields, int topCount, string[] orderFields, bool isDesc) where TModel : ModelBase, new()
        {
            return GetListTopCount<TModel>(topCount, null, null, orderFields, isDesc, selectFields);
        }

        public virtual List<TModel> GetListTopCount<TModel>(string[] selectFields, int topCount, WhereHelper dbWhereModel) where TModel : ModelBase, new()
        {
            return GetListTopCount<TModel>(selectFields, topCount, null, false, dbWhereModel);
        }
        public virtual List<TModel> GetListTopCount<TModel>(string[] selectFields, int topCount) where TModel : ModelBase, new()
        {
            return GetListTopCount<TModel>(selectFields, topCount, null, false);
        }

        public virtual List<TModel> GetListTopCount<TModel>(int topCount, WhereHelper dbWhereModel) where TModel : ModelBase, new()
        {
            return GetListTopCount<TModel>(null, topCount, null, false, dbWhereModel);
        }
        public virtual List<TModel> GetListTopCount<TModel>(int topCount) where TModel : ModelBase, new()
        {
            return GetListTopCount<TModel>(null, topCount, null, false);
        }

        #endregion

        #endregion

        #region 执行存储过程

        public int ExecProcedureGetNon(ProcedureObjBase procObj)
        {
            return _DAL.ExecProcedureGetNon(procObj);
        }

        public DataTable ExecProcedureGetDataTable(ProcedureObjBase procObj)
        {
            return _DAL.ExecProcedureGetDataTable(procObj);
        }

        public object ExecProcedureGetObj(ProcedureObjBase procObj)
        {
            return _DAL.ExecProcedureGetObj(procObj);
        }

        #endregion
    }
}